
/* 调用方法 $("#formId").formData();
 * 1.多个表单 返回数据格式为 {"forms":[{"id":1},{"id":1},{"id":1},{"id":1}]}
 * 2.将form表单元素name-vlue(名称-值)封装为json对象，
 * 3.根据name确定元素，多个相同名字的元素的值默认用英文逗号分开（,）;
 * 4.radio,select,checkbox等元素没有选中则不封装
 * 例如：{"name":"Tom","age":10,"love":"sing,dance,climb mountain,swimming"} 
 */
;(function(){
	$.fn.extend({
		"serializable" :function(separator){
		   var datas =[];
			$(this).each(function(){   
				   var param = $(this).serializeArray();
				   var formData ={}; 
				   
				   for(var index =0 ;index < param.length ;index++){
					 
					   var newValue = $(param[index]).attr('value');
					   
					   var name = $(param[index]).attr('name');
					   
					   if(formData.hasOwnProperty(name)){
						   if(separator){
							   
							   formData[name] = formData[name]+( newValue ? newSeparator + newValue : "");
							   
						   }else{
							   if(formData[name]  instanceof Array){
								   
								   formData[name].push(newValue);
								   
							   }else{ 
								   formData[name] = [formData[name],newValue];
							   } 
						   } 
					   }else{
						   formData[name] = newValue;
					   };  
					}
				   formDatas.push(formData);
			});
             if(datas.length == 1){
            		return datas[0];
             }else{
            	    return {forms:JSON.stringify(datas)} ;
             }; 
		}
	});
	
})(jQuery);